package appLoader.soundLibrary
{
	import appLoader.I_Loader;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.*;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
	

	public class LoadSoundItem extends EventDispatcher
	{
		
		public static const SOUND_LOADING:String = "LoadingSound";
		public static const SOUND_PROGRESS:String="Load Progress Update";
		public static const SOUND_LOAD_COMPLETE:String = "Load Sound Complete";

		private var _id:String;
		private var _soundName:String;
		private var _loadURL:URLRequest;
		private var _soundLoaded:Boolean;
		private var _sound:Sound;
		private var _channel:SoundChannel;
		private var _soundContext:SoundLoaderContext;
		private var _itemSizeKb:int;
		private var _itemDuration:Number;
		private var _percent:int=0;

		private static var logger:ILogger = Log.getLogger("libs.LoadSoundItem");

		
		public function LoadSoundItem(id:String, mySound:String)
		{
			
			logger.log(LogEventLevel.INFO, "Sound added: "+ id);

			if (mySound!="") {
				_id=id;
				_soundName=mySound;
				_loadURL = new URLRequest(_soundName);
				_channel = new SoundChannel
				_soundContext = new SoundLoaderContext;
				
				_sound = new Sound;
				_sound.addEventListener(Event.COMPLETE, loadComplete);
				_sound.addEventListener(ProgressEvent.PROGRESS, handleProgress);
				_sound.addEventListener(IOErrorEvent.IO_ERROR, handleError);
			}
			
		}
		public function initLoad():void {
			_soundLoaded=false;
			_sound.load(_loadURL);
		}
		
		private function handleError(event:IOErrorEvent):void
		{
			logger.log(LogEventLevel.ERROR, "This file : "+ _soundName +" can't be loaded");
		}
		
		private function handleProgress(event:ProgressEvent):void {
			_percent=Math.round(event.bytesLoaded/event.bytesTotal*100);
			dispatchEvent(new Event(LoadSoundItem.SOUND_PROGRESS));
		}
		
		private function loadComplete(event:Event):void {
			_soundLoaded=true;
			_itemSizeKb=_sound.bytesTotal;
			dispatchEvent(new Event(LoadSoundItem.SOUND_LOAD_COMPLETE));
		}

		public function get soundName():String
		{
			return _soundName;
		}

		public function set soundName(value:String):void
		{
			_soundName = value;
		}
		
		public function playSound(loops:int):void {
			_channel=_sound.play(0,loops);
		}

		public function get soundLoaded():Boolean
		{
			return _soundLoaded;
		}

		public function set soundLoaded(value:Boolean):void
		{
			_soundLoaded = value;
		}

		public function get id():String
		{
			return _id;
		}

		public function set id(value:String):void
		{
			_id = value;
		}

		public function get itemSizeKb():int
		{
			if (soundLoaded) {
				_itemSizeKb=_sound.bytesTotal;
			} else {
				logger.log(LogEventLevel.INFO, "Sound not loaded");
				_itemSizeKb=0;
			}
			return _itemSizeKb;
		}

		public function set itemSizeKb(value:int):void
		{
			//_itemSizeKb = value;
		}

		public function get itemDuration():Number
		{
			if (soundLoaded) {
				_itemDuration=_sound.length;
			} else {
				logger.log(LogEventLevel.INFO, "Sound not loaded");
				_itemDuration=0;
			}
			return _itemDuration;
		}

		public function set itemDuration(value:Number):void
		{
			//_itemDuration = value;
		}
		
		public function muteOn():void {
			SoundMixer.soundTransform=new SoundTransform(0);
		}
		
		public function muteOff():void{
			SoundMixer.soundTransform=new SoundTransform(1);
		}

		public function get percent():int
		{
			return _percent;
		}

		public function set percent(value:int):void
		{
			_percent = value;
		}
		
		public function getPercentLoaded():int {
			return _percent;
		}
		
		public function isLoaded():Boolean{
			return _soundLoaded;
		}
	}
}